package com.atguigu.yygh.hosp.controller.api;

import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.utils.HttpRequestHelper;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.model.hosp.Department;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController("ApiDepartmentController")
@RequestMapping("/api/hosp")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

//    /department/remove
    @ApiOperation("医院删除")
    @PostMapping("/department/remove")
    public Result<Department> remove(HttpServletRequest request){
//        将接收的参数,转化为Map集合
        Map<String, Object> DepParams = HttpRequestHelper.switchMap(request.getParameterMap());
//        验证signKey
        String sign = (String) DepParams.get("sign");
        String encrypt = MD5.encrypt(sign);
        if (StringUtils.isEmpty(sign) && StringUtils.isEmpty(encrypt) && !encrypt.equals(sign)){
            throw new YyghException(20001,"查询失败");
        }
        departmentService.remove(DepParams);

        return Result.ok();
    }

    @ApiOperation("获取科室的信息")
    @PostMapping("/department/list")
    public Result<Page> getDepartmentList(HttpServletRequest request){
//        将接收的参数,转化为Map集合
        Map<String, Object> DepParams = HttpRequestHelper.switchMap(request.getParameterMap());

//        验证signKey
        String sign = (String) DepParams.get("sign");
        String encrypt = MD5.encrypt(sign);
        if (StringUtils.isEmpty(sign) && StringUtils.isEmpty(encrypt) && !encrypt.equals(sign)){
            throw new YyghException(20001,"查询失败");
        }

        Page<Department> departmentPage =  departmentService.getDepartmentPage(DepParams);
        return Result.ok(departmentPage);
    }

    @ApiOperation("保存科室信息")
    @PostMapping("/saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
//        将接收的参数,转化为Map集合
        Map<String, Object> DepParams = HttpRequestHelper.switchMap(request.getParameterMap());

//        验证signKey
        String sign = (String) DepParams.get("sign");
        String encrypt = MD5.encrypt(sign);
        if (StringUtils.isEmpty(sign) && StringUtils.isEmpty(encrypt) && !encrypt.equals(sign)){
            throw new YyghException(20001,"查询失败");
        }
        departmentService.saveDepartment(DepParams);
//        保存科室信息
        return Result.ok();
    }
}
